﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HDJ.Framework.Core
{
    public static class RandomUtils
    {
        /// <summary>
        /// 使用特定的概率，随机结果,返回是否随机到了这个概率范围内
        /// </summary>
        /// <param name="probability"></param>
        /// <returns></returns>
        public static bool RandomProbability(float probability)
        {
            if (probability <= 0)
                return false;
            if (probability >= 1f)
                return true;

            int tempProbab = (int)(probability * 100);

            int getNum = UnityEngine.Random.Range(0, 100);

            if (getNum <= tempProbab)
                return true;
            else
                return false;

        }
        /// <summary>
        /// 自定义概率，随机返回传入值
        /// </summary>
        /// <param name="probability">传入概率的数组，所有值相加要等于100，如[60,30,10]</param>
        /// <param name="value">与概率数组个数相同的数组，根据随机返回对应的值</param>
        /// <returns></returns>
        public static object ProbabilityRandom(int[] probability, object[] value)
        {
            int r = UnityEngine.Random.Range(0, 100);
            int low = 0;
            for (int i = 0; i < probability.Length; i++)
            {

                if (i > 0)
                    low += probability[i - 1];
                int top = low + probability[i];

                //Debug.Log("low :" + low + " top :" + top + " r :" + r);
                if (r >= low && r < top)
                    return value[i];
            }

            return null;

        }
        /// <summary>
        /// 自定义概率，随机返回传入值
        /// </summary>
        /// <param name="probability">传入概率的数组，所有值相加要等于100，如[60,30,10]</param>
        /// <param name="value">与概率数组个数相同的数组，根据随机返回对应的值</param>
        /// <returns></returns>
        public static object ProbabilityFixRandom(FixRandom random, int[] probability, object[] value)
        {
            int r = random.Range(0, 100);
            int low = 0;
            for (int i = 0; i < probability.Length; i++)
            {

                if (i > 0)
                    low += probability[i - 1];
                int top = low + probability[i];

                //Debug.Log("low :" + low + " top :" + top + " r :" + r);
                if (r >= low && r < top)
                    return value[i];
            }

            return null;

        }
        /// <summary>
        /// 自定义权重，随机返回传入值
        /// </summary>
        /// <param name="weights">传入权重的数组，所有值相加要大于0，如[60,30,10]</param>
        /// <param name="value">与权重数组个数相同的数组，根据随机返回对应的值</param>
        /// <returns></returns>
        public static T WeightFixRandom<T>(FixRandom random, List<int> weights, List<T> value)
        {
            int max = 0;
            for (int i = 0; i < weights.Count; i++)
            {
                max += weights[i];
            }

            int r = random.Range(0, max);
            int low = 0;
            for (int i = 0; i < weights.Count; i++)
            {

                if (i > 0)
                    low += weights[i - 1];
                int top = low + weights[i];

                //Debug.Log("low :" + low + " top :" + top + " r :" + r);
                if (r >= low && r < top)
                    return value[i];
            }

            return default(T);

        }

    }
}
